package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.supply.dl.R;

import android.support.v4.util.ArrayMap;

import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.PickCollectGoodsActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.OrderInAndOutBoundBean;
import com.supply.dl.bean.request.OrderBoundRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 领料收货确认列表
 * <p>
 * Author: tian <br>
 * Time: 2018/6/12 16:07
 */
public class PickCollectGoodSureModle extends ISearchModle<OrderInAndOutBoundBean> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;

        resource = new ArrayList<>();

        adapter = new NormalAdapter<OrderInAndOutBoundBean>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, OrderInAndOutBoundBean data) {
                holder.setText(R.id.name_tv, data.bussTypeName);
                holder.setText(R.id.status_tv, data.noticeOrderStatusName);
                holder.setText(R.id.order_nub_tv, "入库订单号:" + data.noticeOrderNo);
                holder.setText(R.id.erp_nub_tv, "ERP订单号:" + data.erpOrderNo);
                holder.setText(R.id.time_tv, StringUtils.isEmpty(String.valueOf(data.addTime)) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.addTime), DateUtils.DATE_TIME_FORMAT));//建单时间
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_obtainmaterial;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<OrderInAndOutBoundBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, OrderInAndOutBoundBean data, int position) {
                BaseActivity ac = (BaseActivity) context;
                Bundle bundle = new Bundle();
                ac.jumpActivity(PickCollectGoodsActivity.class, bundle);
            }
        });

    }

    @Override
    public void setHintText() {
        hintText = "领料单号，生产订单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "领料收货确认";
    }

    @Override
    public void init(Context context) {
        this.context = context;

        setTitleShow();
        setSearchAdapter();
//        vistData("", "", null);
    }


    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap,  OnVistDataSuccess onVistDataSuccess) {

        OrderBoundRequest requestData = new OrderBoundRequest();

        requestData.key = StringUtils.isCanUse(keyWord) ? keyWord : "";
        requestData.displayOrderStatus = "";
        requestData.bussType = "";
        requestData.companyId = "";
        requestData.factoryId = "";
        requestData.productWorkshopId = "";
        requestData.createTimeStart = "";
        requestData.createTimeEnd = "";

        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getGoodsConfirmList(requestData, String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<OrderInAndOutBoundBean>>>compose(context))
                .subscribe(new BaseObserver<List<OrderInAndOutBoundBean>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<OrderInAndOutBoundBean>> baseInfo) {
                        if (baseInfo != null) {
                            if (baseInfo.getData() != null) {
                                if(pageNum == 1){
                                    resource.clear();
                                }
                                if(baseInfo.getData().size() < 10){
                                    hasMoreData = false;
                                }
                                resource.addAll(baseInfo.getData());
                                adapter.notifyDataSetChanged();
                            }
                        }
                        if(resultListener != null)
                            resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                        if(resultListener != null)
                            resultListener.onResult();
                    }
                });


        Toast.makeText(context, "vistData方法被调用", Toast.LENGTH_SHORT).show();

//        resource.clear();
//
//        ArrayList<String> returnData = new ArrayList<String>();
//        resource.addAll(returnData);
//        adapter.notifyDataSetChanged();
//
//        if (onVistDataSuccess != null)
//            onVistDataSuccess.showUi(resource.size() == 0);
    }


}



